package com.cdream.basic.algorithm.leetcode.linkedlist;

import java.util.LinkedList;

/**
 * @author cdream
 * @date 6/12/21
 */
public class Question445 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        LinkedList<ListNode> stack1 = new LinkedList<>();
        LinkedList<ListNode> stack2 = new LinkedList<>();
        LinkedList<ListNode> res = new LinkedList<>();
        while (l1 != null) {
            stack1.push(l1);
            l1 = l1.next;
        }
        while (l2 != null) {
            stack2.push(l2);
            l2 = l2.next;
        }
        int up = 0;
        while (stack1.size() != 0 || stack2.size() != 0 || up != 0) {
            ListNode e1 = stack1.poll();
            ListNode e2 = stack2.poll();
            int sum = (e1 == null ? 0 : e1.val) + (e2 == null ? 0 : e2.val) + up;
            ListNode resNode = new ListNode(sum % 10);
            up = sum / 10;
            res.push(resNode);
        }
        ListNode dummy = new ListNode(-1);
        ListNode cur = dummy;
        while (res.size() != 0) {
            cur.next = res.poll();
            cur = cur.next;
        }
        return dummy.next;
    }

    public static void main(String[] args) {
        LinkedList<Integer> test = new LinkedList<>();
        test.offer(1);
        test.offer(2);
        test.offer(3);
        System.out.println(test);
        System.out.println(test.poll());
        System.out.println(test.poll());
        System.out.println(test.poll());
    }
}
